package com.wanmait.recruit.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.mapper.PersonInfoMapper;
import com.wanmait.recruit.pojo.Person;
import com.wanmait.recruit.pojo.PersonInfo;
import com.wanmait.recruit.service.PersonInfoService;
import com.wanmait.recruit.util.PageContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("personInfoService")
@Transactional(rollbackFor = Exception.class)
public class PersonInfoServiceImpl implements PersonInfoService {

    @Resource
    PersonInfoMapper personInfoMapper;

    @Override
    public void insert(PersonInfo object) {
        personInfoMapper.insertSelective(object);
    }

    @Override
    public void delete(PersonInfo object) {

    }

    @Override
    public void update(PersonInfo object) {
        personInfoMapper.updateByPrimaryKeySelective(object);
    }

    @Override
    public PageInfo<PersonInfo> search(int pageNum, int pageSize) {
        return null;
    }

    @Override
    public PageInfo<PersonInfo> findAll(Person person) {
        PageHelper.startPage(PageContext.getPageNum(),1);
        List<PersonInfo> personInfos = personInfoMapper.findAll(person.getId());
        PageInfo<PersonInfo> pageInfo = new PageInfo<>(personInfos);
        pageInfo.getList().forEach(personInfo -> {
            Date date = personInfo.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = simpleDateFormat.format(date);
            personInfo.setTimeStr(time);
        });
        return pageInfo;
    }

    @Override
    public PersonInfo findById(Integer id) {
        PersonInfo personInfo = personInfoMapper.selectByPrimaryKey(id);
        Date date = personInfo.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        personInfo.setTimeStr(time);
        return personInfo;
    }
}
